package com.manage.system.svc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.ContextHolderUtils;
import com.manage.system.api.entity.SysDictData;
import com.manage.system.api.request.dict.DictDataAddOrEditParam;
import com.manage.system.api.service.ISysDictDataService;
import com.manage.system.svc.mapper.SysDictDataMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-11-23
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    private final SysDictDataMapper sysDictDataMapper;

    @Override
    public JsonResult findDictDataList(String dictType) {
        LambdaQueryWrapper<SysDictData> lambda = new QueryWrapper<SysDictData>().lambda()
                .eq(SysDictData::getDictType, dictType);
        return JsonResult.success(sysDictDataMapper.selectList(lambda));
    }

    @Override
    public JsonResult batchDelDictData(List<Long> dictDataIds) {
        log.info("系统服务->字典管理->批量删除字典数据->参数: ids:{}", dictDataIds.toString());
        return JsonResult.toJsonResult(sysDictDataMapper.deleteBatchIds(dictDataIds));
    }

    @Override
    public JsonResult addDictData(DictDataAddOrEditParam dictDataAddOrEditParam) {
        log.info("系统服务->字典管理->添加字典数据->参数: {}", dictDataAddOrEditParam.toString());
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(dictDataAddOrEditParam, sysDictData);
        sysDictData.setCreateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysDictDataMapper.insert(sysDictData));
    }

    @Override
    public JsonResult editDictData(DictDataAddOrEditParam dictDataAddOrEditParam) {
        log.info("系统服务->字典管理->修改字典数据->参数: {}", dictDataAddOrEditParam.toString());
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(dictDataAddOrEditParam, sysDictData);
        sysDictData.setUpdateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysDictDataMapper.updateById(sysDictData));
    }
}
